﻿/*!
 * ajax扩展,格式化返回结果
 * 如果激活按纽设置了AANoeffect属性，则没有处理中效果
 * 如果激活按纽设置了AAShowText属性，则在ajax过程中，显示AAShowText文字
 * 如果激活按纽设置了background-picture属性,则在ajax过程中，显示加载图标
 * iecache 是否加随机数
 */ 
 $.extend({
     AjaxAgency: function (url, data, fun,iecache) {
       var ev = window.event || arguments.callee.caller.arguments[0];
        var et;
        if (ev) {
            et = ev.srcElement || ev.target;
        }
        var temstr, tembackgroundImage, tembackgroundPositionX, tembackgroundPositionY;
        if (et && et.tagName) {
            if ((et.tagName == "INPUT" && et.type != "text") || et.tagName == "BUTTON") {
                var jet = $(et);
                if (!jet.attr("AANoeffect")) {
                    jet.attr("disabled", "disabled");
                    temstr = et.value;

                    if (temstr == "" && et.tagName == "BUTTON") {
                        temstr = et.innerHTML;
                    }
                    if (jet.attr("AAShowText")) {
                        et.value = jet.attr("AAShowText");
                    }
                    else {
                        et.value = "处理中";
                    }
                }
            }
            else if (et.tagName == "A") {
                var jet = $(et);
                if (jet.attr("background-picture")) {
                    jet.attr("disabled", "disabled");
                    tembackgroundPositionX = jet.css("backgroundPositionX");
                    tembackgroundPositionY = jet.css("backgroundPositionY");
                    
                    tembackgroundImage = jet.css("background-image");
                    jet.css({ backgroundImage: "url(/static/app/img/loading.gif)", "backgroundPosition": "0px" });
                }
                if (!jet.attr("AANoeffect")) {
                    jet.attr("disabled", "disabled");
                    temstr = et.innerHTML;
                    if (jet.attr("AAShowText")) {
                        et.innerHTML = jet.attr("AAShowText");
                    }
                    else {
                        et.innerHTML = "处理中";
                    }
                }

            }
        }
        if (!iecache) {
            if (url.indexOf("?") == -1) {
                url = url + "?random=" + Math.random();
            }
            else {
                url = url + "&random=" + Math.random();
            }
        }
        
        if(url.indexOf("SysMessageSroll")!=-1)
        {
            url = url.split('?')[0];
        }
        else
        { url = url + "&random=" + Math.random(); }
        $.ajax({
            url: url,
            type: "POST",
            data: data,
            success: function (data2) {
                if (data2.constructor == String && data2.indexOf("[提示]") == 0) {
                    data2.flag = false;
                }
                else {
                    data2.flag = true;
                }
                if (temstr) {
                    $(et).removeAttr("disabled");
                    if (et.tagName == "INPUT") {
                        et.value = temstr;
                    }
                    else if (et.tagName == "A") {
                            et.innerHTML = temstr;                  
                    }
                }
                if (tembackgroundImage) {
                    $(et).removeAttr("disabled");
                    if (et.tagName == "A") {
                        var currentA = $(et);
                        if (currentA.attr("background-picture")) {
                            currentA.css("background-image", tembackgroundImage);
                            currentA.css("backgroundPositionX", tembackgroundPositionX).css("backgroundPositionY", tembackgroundPositionY);
                        }
                    }
                }
                

                fun(data2);
            },
            error: function (e) {
                if (temstr) {
                    $(et).removeAttr("disabled");
                    if (et.tagName == "INPUT") {
                        et.value = temstr;
                    }
                    else if (et.tagName == "A") {
                        et.innerHTML = temstr;
                    }
                }
                if (tembackgroundImage) {
                    $(et).removeAttr("disabled");
                    if (et.tagName == "A") {
                        var currentA = $(et);
                        if (currentA.attr("background-picture")) {
                            currentA.css("background-image", tembackgroundImage);
                            currentA.css("backgroundPositionX", tembackgroundPositionX).css("backgroundPositionY", tembackgroundPositionY);
                        }
                    }
                }
                if (e.status == 0 && e.statusText == "error") {
                    e = "";
                }
                else {
                    e = e.status + ":" + e.statusText;
                }
                fun(e);
            }
        });
    }
});